using System;
using System.Collections.Generic;
using System.Text;

namespace SingleUserWebsite.Business.ControllerSingleton
{
    public sealed class AccessControl
    {
        private static volatile AccessControl instance = null;
        private static object locker = new object();
        private Dictionary<string, string> cache = new Dictionary<string, string>();

        private AccessControl() { }

        /// <summary>
        /// Singleton get instance
        /// </summary>
        public static AccessControl Instance {

            get
            {
                lock (locker)
                {
                    if (instance == null)
                    {
                        instance = new AccessControl();
                    }
                    return instance;
                }
            }
        }

        public bool IsUserOnline(string userID)
        {

            if (cache.ContainsKey(userID))
            {
                return true;
            }
            else { return false; }
        }

        public bool AddToCache(string userID, string userEmail)
        {
            if (cache.ContainsKey(userID))
            {
                return true;
            }
            else
            {
                cache.Add(userID, userEmail);
                return true;
            }
        }

        public bool RemoveFromCache(string userID)
        {
            if (cache.ContainsKey(userID))
            {
                cache.Remove(userID);
                return true;
            }
            else
            {
                return true;   
            }
        }

        public bool IsAnyOneOnline()
        {
            if (cache.Count > 0) { return true; } else { return false; }
        }

        public void ClearCache()
        {
            cache.Clear();
        }

    }
}
